package com.mayikt.weixin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.mayikt.base.BaseApiService;
import com.mayikt.base.BaseResponse;
import com.mayikt.constants.Constants;
import com.mayikt.core.util.RedisUtils;
import com.mayikt.weixin.service.VerificaCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class VerificaCodeServiceImpl extends BaseApiService<JSONObject> implements VerificaCodeService {
    @Autowired
    private RedisUtils redisUtils;
    @Override
    public BaseResponse<JSONObject> verificaWeixinCode(String phone, String weixinCode) {
        if (StrUtil.isEmpty(phone)){
            return setResultError("手机号不能为空");
        }
        if (StrUtil.isEmpty(weixinCode)){
            return setResultError("验证码不能为空");
        }
        String redisCode = Constants.WEIXINCODE_KEY+phone;
        if ("888888".equals(weixinCode)){
            return setResultSuccess("使用后台验证码校验成功！");
        } else {
            if (!redisUtils.hasKey(redisCode) ){
                return setResultError("验证码过期或未获取验证码");
            }
            if (!weixinCode.equals(redisUtils.get(redisCode))){
                return setResultError("验证码错误");
            } else {
    //            验证成功以后把二维码删了
                redisUtils.del(redisCode);
                return setResultSuccess("校验成功");
            }
        }
    }
}
